using System;
using log4net;
using log4net.Core;
using Quiki.ExpressionParsers;

namespace Quiki.Templates
{
    public class TitleTemplate : IWikiTemplate
    {
        public TitleTemplate(ILogger logger)
        {
            this.logger = logger;
        }

        public const string TemplateId = "title";

        public string TemplateName
        {
            get { return TemplateId; }
        }

        public void Expand(WikiTemplateElement element, WikiDocument document, DocumentGraph documentGraph)
        {
            if (log.IsDebugEnabled)
                log.DebugFormat("Expanding document '{0}'", document.FileName);

            if (element.TemplateParameters.Count < 1)
            {
                logger.Log(Level.Warn, "title template is missing the title text");
                return;
            }

            string title = element.TemplateParameters[0];

            WikiTextElement headingElement = new WikiTextElement(
                WikiTextElementType.Heading,
                1,
                title);
            document.RootElement.Remove(element);
            document.RootElement.ChildElements.Insert(0, headingElement);
        }

        public void Format(WikiTemplateElement element, HtmlFormatterContext context)
        {
        }

        private readonly ILogger logger;
        private static readonly ILog log = LogManager.GetLogger(typeof(TitleTemplate));
    }
}